package com.xncoding.aop.annotation.Conditional;

import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class OnSkillCondition extends SpringBootCondition {

    private static final String PROPERTY_NAME = "brother-rooster.skill";

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取所有被自定义条件注解标记的填写的属性值
        Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSkill.class.getName());
        //获取常量值
        String value = attributes.get("value").toString();
        System.out.println(value);
        Environment environment = context.getEnvironment();
        //读取PROPERTY_NAME值
        String property = environment.getProperty(PROPERTY_NAME);
        System.out.println(property);
        ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnSkill.class);
        ConditionMessage conditionMessage = message.foundExactly(value);
        boolean match = value.equals(property);
        return new ConditionOutcome(match, conditionMessage);
    }
}